using System.Drawing;
using System.IO;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ruminate.GUI.Framework;
using Color = Microsoft.Xna.Framework.Color;
using Point = Microsoft.Xna.Framework.Point;
using Rectangle = Microsoft.Xna.Framework.Rectangle;

namespace Ruminate.Utils {    

    /// <summary>
    /// 
    /// </summary>
    public static class Extentions {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="device"></param>
        /// <param name="sourceModification"></param>
        /// <returns></returns>
        public static Texture2D Transform(this Texture2D texture, GraphicsDevice device, RotateFlipType sourceModification) {

            Stream sourceBuffer = new MemoryStream();
            texture.SaveAsPng(sourceBuffer, texture.Width, texture.Height);

            var sourceImage = Image.FromStream(sourceBuffer);

            sourceBuffer = new MemoryStream();
            sourceImage.RotateFlip(sourceModification);
            sourceImage.Save(sourceBuffer, System.Drawing.Imaging.ImageFormat.Png);

            return Texture2D.FromStream(device, sourceBuffer);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="graphicsDevice"></param>
        public static Texture2D ToStencil(this Texture2D texture, GraphicsDevice graphicsDevice) {

            var size = texture.Width * texture.Height;
            var colorData = new Color[size];
            var white = new Texture2D(graphicsDevice, 1, 1);
            white.SetData(new[] { Color.Black });

            var baker = new TextureBaker(
                graphicsDevice,
                new Vector2(texture.Width, texture.Height), 
                RenderState.Fill);

            baker.Draw(white, new Rectangle(0, 0, texture.Width, texture.Height), Color.White);
            baker.Draw(texture, new Rectangle(0, 0, texture.Width, texture.Height), Color.White);

            var holder = baker.GetTexture();
            holder.GetData(colorData);
            for (var i = 0; i < size; i++) {
                if (colorData[i] == Color.Black) {
                    colorData[i] *= 0.0f;
                    colorData[i].A = 0;
                } else {
                    colorData[i] = Color.Black;
                }

            }
            holder.SetData(colorData);
            return holder;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static Vector2 ToVector2(this Point point) {
            return new Vector2(point.X, point.Y);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vector"></param>
        /// <returns></returns>
        public static Point ToPoint(this Vector2 vector) {
            return new Point((int)vector.X, (int)vector.Y);
        }
    }
}
